package com.home766.controller.admin;

import com.home766.entity.ReleaseSystem;
import com.home766.service.ReleaseSystemService;
import net.biz.Message;
import net.biz.controller.shop.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/releaseSystem")
public class ReleaseSystemController extends BaseController {
	
	@Resource(name="releaseSystemServiceImpl")
	private ReleaseSystemService releaseSystemService;
	
	@RequestMapping("/content")
	public String returnPage(ModelMap model){
		ReleaseSystem  releaseSystem = releaseSystemService.getSigle();
		model.addAttribute("releaseSystem",releaseSystem);
		return "/admin/releaseSystem/content";
	}
	@RequestMapping("/addOrUpdate")
	public String addOrUpdate(HttpServletRequest request, ReleaseSystem releaseSystem, RedirectAttributes redirectAttributes, ModelMap model) throws ParseException {
		if(releaseSystem.getState()==null){
			releaseSystem.setState(0);
		}else{
			releaseSystem.setState(1);
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = sdf.parse(request.getParameter("endDate"));
		releaseSystem.setEndDate(date);
		if(releaseSystem.getId()!=null){
			releaseSystemService.update(releaseSystem);
			addFlashMessage(redirectAttributes,new Message(Message.Type.success,"修改成功"));
		}else{
			releaseSystemService.save(releaseSystem);
			addFlashMessage(redirectAttributes,new Message(Message.Type.success,"保存成功"));
		}
		return "redirect:content.jhtml";
	}
	@RequestMapping("/info")
	@ResponseBody
	public Map<String,Object> info(){
		ReleaseSystem releaseSystem = releaseSystemService.getSigle();
		Map<String,Object> map = new HashMap<String,Object>();
		if(releaseSystem!=null){
			map.put("content", releaseSystem.getContent());
			map.put("state", releaseSystem.getState());
		}
		return map;
	}
}
